package factory.abstractfactory;

import java.io.*;

/**
 * @author wcj
 * @Date 2021/6/20 22:31
 * @Version 1.0
 */
public class FactoryCreator {

    private static String getFactoryType() {
        String[] res = new String[2];
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File("src/factory/abstractfactory/properties.txt")));
            String s = null;
            while ((s = br.readLine()) != null) {
                res = s.split(":");
                if ("".equals(res[1])) {
                    throw new IllegalArgumentException("不合法的参数!");
                }
                break;
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return res[1];
    }

    public static AbstractFactory createFactory() {
        try {
            return (AbstractFactory) Class.forName(getFactoryType()).newInstance();
        } catch (Exception e) {
            throw new IllegalArgumentException("反射获取工厂实例失败!");
        }
    }
}
